//
//  Utility.m
//  DrawCircle
//
//  Created by Nguyen Tan Loc on 5/14/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "Utility.h"

@implementation Utility

static Utility *m_Instance = nil;

+(Utility *) sharedUtility
{
    if (m_Instance == nil)
        m_Instance = [[Utility alloc] init];
    return m_Instance;
}

-(id) init
{
    if (self = [super init])
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:PATH_RESOURCE_INFO ofType:@"plist"];
        m_ResourceInfo = [[NSDictionary alloc] initWithContentsOfFile:path];
    }
    return self;
}


-(CCMenuItem *) createMenuItemWithKey:(NSString *)key target:(id)target selector:(SEL)selector
{
    NSDictionary *dic = [m_ResourceInfo objectForKey:key];
    NSString *imageNameUp   = [dic objectForKey:IMAGE_NAME];
    NSString *imageNameDown = [dic objectForKey:IMAGE_NAME_TOUCHED];
    float    imageX         = [[dic objectForKey:IMAGE_X] floatValue];
    float    imageY         = [[dic objectForKey:IMAGE_Y] floatValue];
    
    CCSprite *spriteUp = [CCSprite spriteWithSpriteFrameName:imageNameUp];
    CCSprite *spriteDown;
    if ([imageNameDown isEqual: @""])
        spriteDown = nil;
    else
        spriteDown = [CCSprite spriteWithSpriteFrameName:imageNameDown];
    
    CCMenuItem *menuItem = [CCMenuItemSprite itemWithNormalSprite:spriteUp selectedSprite:spriteDown target:target selector:selector];
    [menuItem setPosition:ccp(imageX, imageY)];
    
    return menuItem;
}


-(CCSprite *) createSpriteWithKey:(NSString *)key
{
    NSDictionary *dic = [m_ResourceInfo objectForKey:key];
    NSString *imageName    = [dic objectForKey:IMAGE_NAME];
    float    imageX         = [[dic objectForKey:IMAGE_X] floatValue];
    float    imageY         = [[dic objectForKey:IMAGE_Y] floatValue];
    
    CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:imageName];
    [sprite setPosition:ccp(imageX, imageY)];
    
    return sprite;
}


-(CCSprite *) createSpriteAnimWithKey:(NSString *)key delay:(float)delay
{
    NSDictionary *dic = [m_ResourceInfo objectForKey:key];
    NSString *imageFirst    = [dic objectForKey:IMAGE_FIRSTFRAME];
    NSString *imageSub      = [dic objectForKey:IMAGE_SUB];
    int      imageNum       = [[dic objectForKey:IMAGE_NUM] intValue];
    float    imageX         = [[dic objectForKey:IMAGE_X] floatValue];
    float    imageY         = [[dic objectForKey:IMAGE_Y] floatValue];
    
    NSMutableArray *animArray = [NSMutableArray array];
    int num;
    for (int i = 1; i <= imageNum; i++)
    {
        if (i < 10)
            num = [NSString stringWithFormat:@"0%d", i];
        else
            num = [NSString stringWithFormat:@"%d", i];
        
        [animArray addObject:[[CCSpriteFrameCache sharedSpriteFrameCache]
                              spriteFrameByName:[NSString stringWithFormat:imageSub, num]]];
    }

    CCAnimation *animation = [CCAnimation animationWithSpriteFrames:animArray delay:delay];
    CCAnimate *animate = [CCAnimate actionWithAnimation:animation];
    CCRepeatForever *action = [CCRepeatForever actionWithAction:animate];
    
    CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:imageFirst];
    [sprite setPosition:ccp(imageX, imageY)];
    [sprite runAction:action];
    
    return sprite;
}

@end

